package org.example.controller.market;


import com.baomidou.mybatisplus.core.metadata.IPage;
import org.example.annotation.HasRole;
import org.example.dao.commom.Result;
import org.example.dao.commom.ResultCode;
import org.example.dao.entity.market.CImage;
import org.example.dao.entity.market.CStore;
import org.example.dao.vo.CImageVo;
import org.example.exception.ParamLossErrorException;
import org.example.service.market.StoreService;
import org.example.skytool.commom.ParamUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author 22449
 */
@RestController
@RequestMapping("/market")
@CrossOrigin
public class StoreController {

    @Resource
    private StoreService storeService;

    /**
     * 为某个商户创建店铺(管理)
     */
    @PostMapping("addStore")
    @HasRole(role = "管理员")
    public Result addStore(@RequestBody CStore store) {
        Long businessId = store.getBusinessId();
        ParamUtil.LongParam(businessId);
        store = storeService.addStore(store);
        return new Result(ResultCode.SUCCESS, store);
    }

    /**
     * 创建店铺(商户)
     */
    @PostMapping("createStore")
    @HasRole(role = "商户")
    public Result createStore(@RequestBody CStore store) {
        store = storeService.createStore(store);
        return new Result(ResultCode.SUCCESS, store);
    }

    /**
     * 查询本人店铺(商户)
     */
    @PostMapping("selectStoreByMine/{limit}/{start}")
    @HasRole(role = "商户")
    public Result selectStoreByMine(
            @PathVariable("limit") Long limit,
            @PathVariable("start") Long start) {
        ParamUtil.LongParam(limit);
        ParamUtil.LongParam(start);
        IPage<CStore> page = storeService.selectStoreByMine(limit, start);
        return new Result(ResultCode.SUCCESS, page);
    }

    /**
     * 条件查询本人店铺(商户)
     */
    @PostMapping("selectStoreByConditionByMine/{limit}/{start}")
    @HasRole(role = "商户")
    public Result selectStoreByConditionByMine(
            @PathVariable("limit") Long limit,
            @PathVariable("start") Long start) {
        ParamUtil.LongParam(limit);
        ParamUtil.LongParam(start);
        return new Result(ResultCode.SUCCESS, null);
    }

    /**
     * 查询店铺(管理)
     */
    @PostMapping("selectStore/{limit}/{start}")
    @HasRole(role = "管理员")
    public Result selectStore(@PathVariable("limit") Long limit,
                              @PathVariable("start") Long start) {
        ParamUtil.LongParam(limit);
        ParamUtil.LongParam(start);
        IPage<CStore> page = storeService.selectStore(limit, start);
        return new Result(ResultCode.SUCCESS, page);
    }

    /**
     * 查询店铺(用户)
     */
    @PostMapping("selectStoreForUser/{limit}/{start}")
    @HasRole(role = "用户")
    public Result selectStoreForUser(@PathVariable("limit") Long limit,
                              @PathVariable("start") Long start) {
        ParamUtil.LongParam(limit);
        ParamUtil.LongParam(start);
        IPage<CStore> page = storeService.selectStoreForUser(limit, start);
        return new Result(ResultCode.SUCCESS, page);
    }

    /**
     * 条件查询商铺(管理)
     */
    @PostMapping("selectStoreByCondition/{limit}/{start}")
    @HasRole(role = "管理员")
    public Result selectStoreByCondition(@PathVariable("limit") Long limit,
                                         @PathVariable("start") Long start) {
        ParamUtil.LongParam(limit);
        ParamUtil.LongParam(start);
        return new Result(ResultCode.SUCCESS, null);
    }

    /**
     * 修改店铺信息(商户)
     */
    @PostMapping("updateStore")
    @HasRole(role = "商户")
    public Result updateStore(@RequestBody CStore store) {
        Long id = store.getId();
        ParamUtil.LongParam(id);
        storeService.checkStore(id);
        store = storeService.updateStore(store);
        return new Result(ResultCode.SUCCESS, store);
    }

    /**
     * 修改店铺主页图像(商户)
     */
    @PostMapping("updateStoreIcon/{storeId}")
    @HasRole(role = "商户")
    public Result updateStoreIcon(@PathVariable("storeId") Long storeId, MultipartFile file) {
        if (file == null || file.isEmpty()) {
            throw new ParamLossErrorException("文件未上传");
        }
        storeService.checkStore(storeId);
        CStore cStore = storeService.updateStoreIcon(storeId, file);
        return new Result(ResultCode.SUCCESS, cStore);
    }

    /**
     * 上传店铺图像(商户)
     */
    @PostMapping("uploadStoreImages/{storeId}")
    @HasRole(role = "商户")
    public Result uploadStoreImages(@RequestBody MultipartFile[] files, @PathVariable Long storeId) {
        ParamUtil.LongParam(storeId);
        List<CImage> cImageList = storeService.uploadStoreImages(files, storeId);
        return new Result(ResultCode.SUCCESS, cImageList);
    }

    /**
     * 查看店铺图像(商户用户通用)
     */
    @PostMapping("selectStoreImages")
    @HasRole(role = "商户")
    public Result selectStoreImages(@RequestBody CImageVo cImageVo) {
        Long storeId = cImageVo.getStoreId();
        ParamUtil.LongParam(storeId);
        List<CImage> cImageList = storeService.selectStoreImages(storeId);
        return new Result(ResultCode.SUCCESS, cImageList);
    }

    /**
     * 删除店铺(管理)
     */
    @PostMapping("deleteStore")
    @HasRole(role = "管理员")
    public Result deleteStore(@RequestBody CStore store) {
        Long id = store.getId();
        ParamUtil.LongParam(id);
        storeService.deleteStore(store);
        return new Result(ResultCode.SUCCESS, null);
    }


}
